<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
	<!-- 商品列表 -->
	<GoodsList :goodsList="guessList" :finish="finish"/>
</template>

<script setup lang="ts">
import {onLoad} from '@dcloudio/uni-app'
import GoodsList from './GoodsList.vue'
import { getGoodsGuessLikeAPI } from '@/services/home'
import type { Data, GoodsItem, PageParams, PageResult } from '@/types/global.d'
import type { GuessItem } from '@/types/home.d'
import { onMounted, ref } from 'vue'

// 分页参数
const pageParams: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}
// 猜你喜欢的列表
const guessList = ref<GuessItem[]>([])
// 已结束标记
const finish = ref(false)

// 获取猜你喜欢数据
async function getGoodsGuessLikeData(){
  // 退出分页判断
  if (finish.value === true) {
    return uni.showToast({ icon: 'none', title: '没有更多数据~' })
  }
	//传入分页参数
  const res:any = await getGoodsGuessLikeAPI(pageParams)
  // 数组追加
  guessList.value.push(...res.result.items)
  // 分页条件res.result.pages记录总页数
  if (pageParams.page < res.result.pages) {
    // 页码累加
    pageParams.page++
  } else {
    finish.value = true
  }
}

//重置组件数据
function resetData(){
	pageParams.page = 1
	guessList.value = []
	finish.value = false
}
// 组件挂载完毕,自动调用api
onLoad(() => {
  getGoodsGuessLikeData()
})

// 暴露方法 组件通讯 
//使用 ref：适合父组件主动调用子组件的方法，逻辑清晰且直接。
//使用 emit：适合子组件主动通知父组件，解耦性更强。
defineExpose({
  getMore: getGoodsGuessLikeData,
	resetData
})
</script>

<style lang="scss">
:host {
  display: block;
}
/* 分类标题 */
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  padding: 36rpx 0 40rpx;
  font-size: 32rpx;
  color: #262626;
  .text {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;

    &::before,
    &::after {
      content: '';
      width: 20rpx;
      height: 20rpx;
      background-image: url(@/static/images/bubble.png);
      background-size: contain;
      margin: 0 10rpx;
    }
  }
}
</style>
